package com.eptok.gw.szcups.service;

import org.dom4j.Document;
import org.springframework.web.context.ContextLoader;

import ys8583.parse.IOMessage;

import com.eptok.gw.szcups.cups.CupsMessageControl;
import com.eptok.gw.szcups.cups.message.PackMessageUtil;
import com.eptok.gw.szcups.dao.JdbcDao;
import com.eptok.gw.szcups.pojo.Reversal;
import com.eptok.gw.szcups.pojo.XmlMessage;
import com.eptok.gw.szcups.socket.client.factory.ClientFactory;
import com.eptok.gw.szcups.util.SerialNumber;

/**
 * 冲正
 * @author guoq
 *
 */
public class ReversalService  extends BaseService{
	
	public ReversalService(){
	}

	@Override
	public IOMessage process(XmlMessage xmlMessage) throws Exception {
		Reversal pay = (Reversal)xmlMessage;
		JdbcDao jdbcDao = (JdbcDao)webApplicationContext.getBean("jdbcDao");
		String hexData = jdbcDao.queryHexDataByOldRequireId(pay.getMerchantId(), pay.getOldReqSerialNumber());
		IOMessage iomessagersp = null;
		if(hexData == null){
			pay.setStatus("Z7");
			pay.setRspSerialNumber(SerialNumber.seqno());
		}else{
			IOMessage iomessagereq = PackMessageUtil.packReversal(hexData);
			long id = this.insertlog(pay, iomessagereq);
			ClientFactory.getInstance().sendMessage(CupsMessageControl.CHAN_CLIENT_KEY, iomessagereq);
			iomessagersp = CupsMessageControl.getInstance().getChanMessageRsp((String)iomessagereq.getRspField(11));
			if(iomessagersp == null){
				//冲正超时直接返回结果Z2
				pay.setStatus("Z2");
				pay.setRspSerialNumber(SerialNumber.seqno());
			}else{
				pay.setStatus((String)iomessagersp.getReqMesField(39));
				pay.setRspSerialNumber(SerialNumber.seqno());
			}
			this.updatelog(id, pay, iomessagersp);
		}
		return iomessagersp;
	}

	
	

}
